﻿using MVVMDemo.ViewModels;
using System.Windows;

namespace MVVMDemo.Views
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        #region AddCommand

        private void AddCommandCanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            var viewModel = Resources["ViewModel"] as MainWindowViewModel;
            if (viewModel == null)
            {
                throw new NullReferenceException("ViewModel can't be NULL");
            }

            var userDetailts = viewModel.NewUserDetails;
            e.CanExecute = !(string.IsNullOrWhiteSpace(userDetailts.Firstname) || string.IsNullOrWhiteSpace(userDetailts.Lastname));
        }

        private void AddCommandExecute(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            var viewModel = Resources["ViewModel"] as MainWindowViewModel; 
            
            viewModel.UserCollection.Add(viewModel.NewUserDetails);
            viewModel.SelectedUser = viewModel.NewUserDetails;
            viewModel.NewUserDetails = new Models.UserModel();
        }

        #endregion AddCommand
    }
}
